package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.LoginException;
import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.utils.FilesUtils;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.mapper.zjs.AccountMapper;
import com.fantastic.mapper.zjs.UserMapper;
import com.fantastic.model.pojo.zjs.Account;
import com.fantastic.model.pojo.zjs.User;
import com.fantastic.model.vo.zjs.UserVo;
import com.fantastic.service.zjs.AuthService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: rxx
 * @since: 2024/6/14 10:54
 * @description: 后台身份验证
 */
@Service("adminAuth")
public class AuthServiceAdminImpl implements AuthService {

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 登录
     *
     * @param account 账号对象
     * @return 用户信息
     */
    @Override
    public UserVo login(Account account) {
        IsEmptyUtils.stringIsEmpty(new SelectException(RespStatusInfo.LOGIN_ERROR), account.getAccount(),
                account.getPwd());
        Account loginAccount = accountMapper.loginAccount(account);
        if (loginAccount != null) {
            if (loginAccount.getAccountId() != null) {
                if (loginAccount.getAccountStatus() != 0) {
                    //返回用户信息
                    UserVo userVo = new UserVo();
                    userVo.setUser(userMapper.getUser(loginAccount.getUserId()));
                    userVo.getUser().setUserPicture(FilesUtils.getSlashParse(userVo.getUser().getUserPicture()));
                    return userVo;
                }
                //抛出账号封禁
                throw new LoginException(RespStatusInfo.ACCOUNT_ERROR);
            } else {
                //抛出登录失败异常
                throw new LoginException(RespStatusInfo.LOGIN_ERROR);
            }
        }
        //抛出登录失败异常
        throw new LoginException(RespStatusInfo.LOGIN_ERROR);
    }
}
